﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iLangl.Model;
using System.Data;

namespace iLangl.BusinessModel.DAL.XMLStorage
{
	public class ProjectDAL : BaseStorageDAL, IProjectDAL
	{
		#region IProjectDAL Members

		public void CreateProject(Project project, IDbTransaction transaction)
		{
			dtoXmlStorage.ProjectRow projectRow = Storage.Project.FindById(project.Id);
			bool isNew = (projectRow == null);
			bool isChanged = false;
			if (isNew)
			{
				projectRow = Storage.Project.NewProjectRow();
				isChanged = true;
			}

			if (!isChanged)
			{
				isChanged |= !project.Id.Equals(projectRow.Id);
				if ((!projectRow.IsNameNull()) && (!String.IsNullOrEmpty(project.Name)))
					isChanged |= !project.Name.Equals(projectRow.Name);

				if (!projectRow.IsDeadlineNull())
					isChanged |= !project.Deadline.Equals(projectRow.Deadline);

				if ((!projectRow.IsDescriptionNull()) && (!String.IsNullOrEmpty(project.Description)))
					isChanged |= !project.Description.Equals(projectRow.Description);
				if ((!projectRow.IsCustomerIdentityNull()) && (!String.IsNullOrEmpty(project.CustomerIdentity)))
					isChanged |= !project.CustomerIdentity.Equals(projectRow.CustomerIdentity);
				if ((!projectRow.IsTPIdentityNull()) && (!String.IsNullOrEmpty(project.TPIdentity)))
					isChanged |= !project.TPIdentity.Equals(projectRow.TPIdentity);
				if ((!projectRow.IsStatusNull()) && (!String.IsNullOrEmpty(project.Status)))
					isChanged |= !project.Status.Equals(projectRow.Status);
				if ((!projectRow.IsTPIdentityNull()) && (!String.IsNullOrEmpty(project.TPIdentity)))
					isChanged |= !project.TPIdentity.Equals(projectRow.TPIdentity);
				if ((!projectRow.IsLicenseKeyNull()) && (!String.IsNullOrEmpty(project.LicenseKey)))
					isChanged |= !project.LicenseKey.Equals(projectRow.LicenseKey);

				if (project.Owner != null)
				{
					if (!projectRow.IsOwnerIdNull())
						isChanged |= !project.Owner.Id.Equals(projectRow.OwnerId);
				}

				if (project.Product != null)
				{
					if (!projectRow.IsProductIdNull())
						isChanged |= !project.Product.Id.Equals(projectRow.ProductId);
				}

				if (project.TechnicalContact != null)
				{
					if (!projectRow.IsTechnicalContactIdNull())
						isChanged |= !project.TechnicalContact.Id.Equals(projectRow.TechnicalContactId);
				}

				if (project.WorkArea != null)
				{
					if (!projectRow.IsWorkAreaIdNull())
						isChanged |= !project.WorkArea.Id.Equals(projectRow.WorkAreaId);
				}
			}

			projectRow.Id = project.Id;

			if(!String.IsNullOrEmpty(project.Name))
				projectRow.Name = project.Name;

			if (!String.IsNullOrEmpty(project.Description))
				projectRow.Description = project.Description;
			if (!String.IsNullOrEmpty(project.CustomerIdentity))
				projectRow.CustomerIdentity = project.CustomerIdentity;
			if (!String.IsNullOrEmpty(project.TPIdentity))
				projectRow.TPIdentity = project.TPIdentity;
			if (!String.IsNullOrEmpty(project.Status))
				projectRow.Status = project.Status;
			if (!String.IsNullOrEmpty(project.LicenseKey))
				projectRow.LicenseKey = project.LicenseKey;

			if (project.Owner != null)
			{
				projectRow.OwnerId = project.Owner.Id;
			}

			if (project.Product != null)
			{
				projectRow.ProductId = project.Product.Id;
			}

			if (project.TechnicalContact != null)
			{
				projectRow.TechnicalContactId = project.TechnicalContact.Id;
			}

			if (project.WorkArea != null)
			{
				projectRow.WorkAreaId = project.WorkArea.Id;
			}

			if (isNew)
			{
				Storage.Project.AddProjectRow(projectRow);
			}

			if (isChanged)
			{
				projectRow.TimeSpan = CurrentTimeSpan;
			}

			if (transaction == null)
			{
				AcceptChanges();
			}
		}


		public Project ReadProject(string projectId)
		{
			Project result = (Project)Facade.Instance.ModelFactory.CreateProject();
			dtoXmlStorage.ProjectRow projectRow = Storage.Project.FindById(projectId);

			if (projectRow == null)
			{
				throw new ApplicationException(String.Format("Project with Id='{0}' not found!", projectId));
			}

			result.Id = projectRow.Id;
			result.Deadline = projectRow.Deadline;
			result.Description = projectRow.Description;
			result.Name = projectRow.Name;
			result.LicenseKey = projectRow.LicenseKey;

			result.WorkArea = DALFacade.Instance.WorkAreaDAL.ReadWorkAreaById(projectRow.WorkAreaId);
			result.Owner = DALFacade.Instance.ContactDAL.ReadContact(projectRow.OwnerId);
			result.TechnicalContact = DALFacade.Instance.ContactDAL.ReadContact(projectRow.TechnicalContactId);
			result.Product = DALFacade.Instance.ProductDAL.ReadProductById(projectRow.ProductId);


			if (!projectRow.IsCustomerIdentityNull())
				result.CustomerIdentity = projectRow.CustomerIdentity;

			if (!projectRow.IsTPIdentityNull())
				result.TPIdentity = projectRow.TPIdentity;

			return result;
		}
	

		public void UpdateProject(Project project, IDbTransaction transaction)
		{
			CreateProject(project, transaction);
		}

		#endregion
	}
}